$(function () {
    var userId = $("#userId").val();
    var settings = {
        async: {
            dataType: 'json',
            dataFilter: function (treeId, treeNode, response) {
                if (response.success==true) {
                    return response.entity;
                } else {
                    icModale(0, response.message);
                }
            },
            enable: true,
            type: 'post',
            url: baseloaction + "/admin/manage/user/ajaxSysFunctionList?userId="+userId
        },
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        data: {
            key: {
                name: 'functionName',
                title: 'functionName',
                functionType: 'functionType'
            },
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'parentId',
                rootPId: null
            }
        },
        view: {
            showLine: true,
            fontCss: function (treeId, treeNode) {
                if (typeof treeNode.highlight == 'undefined' || treeNode.highlight == false) {
                    return {color: "#333", "font-weight": "normal"};
                } else {
                    return {color: "#A60000", "font-weight": "bold"};
                }
            }
        },
        callback: {
            onAsyncError: function () {
                icModale(0, "系统错误，权限获取失败");
            },
            onAsyncSuccess: function (event, treeId) {
                //初始化权限树
                var treeObj = $.fn.zTree.getZTreeObj(treeId);
                //展开所有的节点
                treeObj.expandAll(treeObj);
                //根要节点
                var parentNodeList = treeObj.getNodes();
                //设置节点选中
                setNodeSelected(treeObj,parentNodeList);
                //设置节点禁用
                setNodeDisabled(treeObj,parentNodeList);
            }
        }
    };
    //初始化权限树
    $.fn.zTree.init($('#function'), settings);

    $("body").on('keyup', '#search-function', function () { //搜索权限树
        var keyword = this.value;
        var treeObj = $.fn.zTree.getZTreeObj("function");
        if ($.isEmptyObject($.trim(keyword))) {
            var nodes = treeObj.getNodesByFilter(function (node) {
                return node.highlight;
            }, false);
            if (nodes.length > 0) {
                nodes.forEach(function (node) {
                    node.highlight = false;
                    treeObj.updateNode(node);
                });
            }
            return;
        }
        treeObj.getNodesByFilter(function (node) {
            return node.highlight;
        }, false).forEach(function (node) {
            node.highlight = false;
            treeObj.updateNode(node);
        });
        treeObj.getNodesByFilter(function (node) {
            return node.functionName.indexOf(keyword) != -1;
        }).forEach(function (node) {
            node.highlight = true;
            treeObj.updateNode(node);
            treeObj.expandNode(node,true,true,true);
            var parentNode = node.getParentNode();
            if(parentNode!=null && parentNode.isHidden){
                treeObj.expandNode(parentNode,true,true,true);
            }
        });
    });
});

/**
 * 设置节点禁用
 * @param treeObj
 * @param nodeList
 */
function setNodeDisabled(treeObj,nodeList) {
    $.each(nodeList,function (index,node) {
        var childList = node.children;
        if(typeof childList !='undefined' && childList.length > 0){
            setNodeDisabled(treeObj,childList);
        }
        if(node.disabled==true){
            treeObj.setChkDisabled(node,true);
        }
    });
}

/**
 * 设置节点选中
 * @param treeObj
 * @param nodeList
 */
function setNodeSelected(treeObj,nodeList) {
    $.each(nodeList,function (index,node) {
        var childList = node.children;
        if(typeof childList !='undefined' && childList.length > 0){
            setNodeSelected(treeObj,childList);
        }else{
            if(node.selected==true){
                treeObj.checkNode(node,true,true);
            }
        }
    });
}

/**
 * 获取权限ID串
 * @returns {string}
 */
function getFunctionData() {
    var treeObj = $.fn.zTree.getZTreeObj("function");
    var nodes = treeObj.getCheckedNodes(true);
    var functionIds = '';
    if(nodes!=null && nodes.length > 0){
        var list = new Array();
        $.each(nodes,function (index,node) {
           list.push(node.id);
        });
        functionIds = list.join(",");
    }
    return functionIds;
}